package com.tkk.monitoring.analyze.config;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2015/7/9.
 */
public class Regexp extends Config {

    private List<Pattern> patterns = new LinkedList<Pattern>();

    public Regexp(String filePath) {
        super(filePath);
        try {
            List<String> strings = IOUtils.readLines(getInputStream());
            for (String string : strings) {
                patterns.add(Pattern.compile(string, Pattern.CASE_INSENSITIVE));
            }
        } catch (IOException e) {
        }
    }

    public boolean isMatch(String org) {
        for (Pattern pattern : patterns) {
            if (pattern.matcher(org).find()) {
                return true;
            }
        }
        return false;
    }

    public boolean isNotMatch(String org) {
        return !isMatch(org);
    }
}
